package com.nononsenseapps.feeder.pad.ui.feed

import android.content.Intent
import android.os.*
import android.view.View
import androidx.core.view.isVisible
import androidx.navigation.fragment.findNavController
import com.nononsenseapps.feeder.db.SYSTEM_FEED_LIST
import com.nononsenseapps.feeder.pad.R
import com.nononsenseapps.feeder.pad.ui.FeedActivity
import com.nononsenseapps.feeder.ui.feed.AbstractFeedFragment
import com.nononsenseapps.feeder.ui.reader.webview.ARG_URL
import com.nononsenseapps.feeder.util.*

class FeedFragment : AbstractFeedFragment() {

    var maxPanelModel=false
    private var notificationModel=false

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        adapter.expandable=false
    }

    fun showFeedCommandFab(){
        (activity as FeedActivity).setFabImageById(R.drawable.ic_forward_top)
        //check all button
        (activity as FeedActivity).fabOnClickListener = {
            when(emptyView?.isVisible){
                true->(activity as FeedActivity).openNavDrawer()
                false-> recyclerView?.smoothScrollToPosition(0)
            }
        }

        //check all button
        (activity as FeedActivity).fabOnDoubleClickListener= {
            if(id !in SYSTEM_FEED_LIST)
                requestFeedSyncRead()
        }

        //check all button
        (activity as FeedActivity).fabOnLongClickListener= {
            when(emptyView?.isVisible){
                true->requestFeedSyncAll()
                false-> {
                    adapter.currentList?.let{
                        markCurrentListAsRead(it.toList())
                        context?.makeToast(getString(R.string.mark_all_as_read))
                        if(id !in SYSTEM_FEED_LIST)
                            requestFeedSyncRead()
                    }

                }
            }
        }
    }


    override fun currentModelChanged() {
        super.currentModelChanged()
        notificationModel.notTrue {
            navigateToWelcomeFragment()
        }
    }

    private fun navigateToWelcomeFragment(){
        if(!maxPanelModel)
            findNavController().navigate(R.id.welcomeFragment)
    }

    fun changeToMaxPanelModel(maxPanelModel:Boolean){
        if(maxPanelModel){
            this.maxPanelModel =true
            adapter.expandable=true
            adapter.shadowAble=true
            changeToStaggeredGridLayoutManager(largeScreen = true)
        }else{
            this.maxPanelModel =false
            adapter.expandable=false
            adapter.shadowAble=false
            changeToLinearLayoutManager()
        }
    }

    override fun openFeedsList() {
        (activity as FeedActivity).openNavDrawer()
    }

    override fun getActivityIntent(): Intent =Intent(context, FeedActivity::class.java)

    override fun debug():Boolean=true

    override fun getUpdateXmlUrl(): String ="https://raw.githubusercontent.com/wushunlian/app/master/feeder/pad-update-changelog.xml"

    override fun navigateToFeedFragment(id: Long, tag: String?) {
        notificationModel=false
        initModel(id,tag)
    }

    override fun navigateToSettingFragment() {
        findNavController().navigate(if(maxPanelModel) R.id.action_feedFragment_to_settingsFragmentDialog else R.id.action_feedFragment_to_settingsFragment)
    }

    override fun navigateToFeedHotEditFragment() {
        findNavController().navigate(if(maxPanelModel) R.id.action_feedFragment_to_feedHotEditFragmentDialog else R.id.action_feedFragment_to_feedHotEditFragment)
    }

    override fun navigateToReaderFragment() {
        findNavController().navigate(if(maxPanelModel) R.id.action_feedFragment_to_readerFragmentDialog else R.id.action_feedFragment_to_readerFragment)
    }

    override fun navigateToReaderWebViewFragment(link: String?, enclosureLink: String?) {
        findNavController().navigate(
                if(maxPanelModel)  R.id.action_feedFragment_to_readerWebViewFragmentDialog else  R.id.action_feedFragment_to_readerWebFragment,
                bundle {
                    putString(ARG_URL, link)
                    putString(com.nononsenseapps.feeder.ui.reader.sample.ARG_ENCLOSURE, enclosureLink)
                }
        )
    }

    fun notificationToReaderFragment(feedId: Long, itemId:Long) {
        notificationModel=true
        initModel(feedId,tag)
        findNavController().navigate(if(maxPanelModel) R.id.action_feedFragment_to_readerFragmentDialog else R.id.action_feedFragment_to_readerFragment,bundle {
                putLong(ARG_ID, itemId)
        })
    }

    fun notificationToWebViewFragment(feedId: Long, link:String?,enclosureLink: String?) {
        notificationModel=true
        initModel(feedId,tag)
        navigateToReaderWebViewFragment(link,enclosureLink)
    }

    override fun navigateToFeedRuleFragment(feedId: Long?, regContent: String?, adFlag: Boolean?) {
        findNavController().navigate(R.id.action_global_feedRuleEditorDialog, bundle {
            feedId?.let { putLong(ARG_FEED_ID, feedId)}
            regContent?.let {  putString("regContext", regContent)}
            adFlag?.let {  putBoolean("adFlag",adFlag) }
        })
    }
}
